package com.x.mvp.utils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RandomUtil {

    private static Random random = new Random(System.currentTimeMillis());

    /**
     * Random generate an integer in [0, range)
     *
     * @param range
     * @return
     */
    public static int uniform(int range) {
        return uniform(0, range);
    }

    public static void seed(long seed) {
        random = new Random(seed);
    }

    /**
     * Random generate an integer in [min, max)
     *
     * @param min
     * @param max
     * @return
     */
    public static int uniform(int min, int max) {
        return min + random.nextInt(max - min);
    }

    /**
     * Generate issueNo repeat {@code size} indexes from {@code min} to {@code max}
     */
    public static int[] indexs(int size, int min, int max) {
        Set<Integer> used = new HashSet<>();
        int[] index = new int[size];

        for (int i = 0; i < index.length; i++) {
            while (true) {
                int ind = uniform(min, max);
                if (!used.contains(ind)) {
                    index[i] = ind;
                    used.add(ind);
                    break;
                }
            }
        }
        return index;
    }
}
